package com.njlime.cms.service.sys;

import com.njlime.cms.entities.Channel;
import com.njlime.cms.entities.ChannelColumn;
import com.njlime.cms.repository.sys.ChannelColumnDao;
import com.njlime.cms.repository.sys.ChannelDao;
import com.njlime.core.base.entities.BaseEntity;
import com.njlime.core.helper.GlobalValue;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by Dean on 2017-8-10.
 */
@Service
@Transactional(readOnly = true)
public class ChannelService {

    @Resource
    private ChannelDao channelDao;
    @Resource
    private InformationService informationService;
    @Resource
    private ChannelColumnDao channelColumnDao;
    @Resource
    private ColumnService columnService;

    /**
     * 详情
     *
     * @param id
     * @return
     */
    public Channel findOne(int id) {
        return channelDao.findOne(id);
    }

    /**
     * 根据单位和序号长频道
     *
     * @param unitId       单位id
     * @param channelShort 序号
     * @return
     */
    public Map<String, Object> getChannelByShort(int unitId, int channelShort) {

        Map<String, Object> map1 = channelDao.getChannelByShort(unitId, channelShort);
        if (map1 != null) {
            map1.put("columnList", columnService.getChannelColumnListByChannelId(Integer.parseInt(map1.get("id").toString()), unitId));
        }
        return map1;
    }

    /**
     * 根据单位和序号长频道
     *
     * @param unitId 单位id
     * @param channelId 序号
     * @return
     */
    public Map<String, Object> getChannelById(int unitId, int channelId) {

        Map<String, Object> map1 = channelDao.getChannelById(unitId, channelId);
        if (map1 != null) {
            map1.put("columnList", columnService.getChannelColumnListByChannelId(Integer.parseInt(map1.get("id").toString()), unitId));
        }
        return map1;
    }


    @Transactional
    public void setting(Channel info){
        channelDao.settingColor(info);
    }


    /**
     * 保存
     *
     * @param info
     * @param columns
     */
    @Transactional(readOnly = false)
    public void save(Channel info, String columns) {
        channelDao.save(info);
        channelColumnDao.deleteAll(info.getId(), info.getUnit_id());
        if (columns != null && !columns.equals("")) {
            String arr[] = columns.split(",");
            int i = 1;
            if (columns != null && !columns.equals("")) {
                for (String str : arr) {
                    if (!str.equals("")) {
                        ChannelColumn cc = new ChannelColumn();
                        cc.setShort_num(i++);
                        cc.setChannel_id(info.getId());
                        cc.setChannel_short(info.getChannel_short());
                        cc.setT_column_id(Integer.parseInt(str));
                        cc.setUnit_id(info.getUnit_id());
                        channelColumnDao.save(cc);
                    }
                }
            }
        }
    }

    /**
     * 更新
     *
     * @param info
     * @param columns
     */
    @Transactional(readOnly = false)
    public void update(Channel info, String columns) {
        channelDao.update(info);
        channelColumnDao.deleteAll(info.getId(), info.getUnit_id());
        if (columns != null && !columns.equals("")) {
            String arr[] = columns.split(",");
            List<String> list = new ArrayList<String>();
            int i = 1;
            for (String str : arr) {
                list.add(str);
                ChannelColumn cc = new ChannelColumn();
                cc.setShort_num(i++);
                cc.setChannel_id(info.getId());
                cc.setChannel_short(info.getChannel_short());
                cc.setT_column_id(Integer.parseInt(str));
                cc.setUnit_id(info.getUnit_id());
                channelColumnDao.save(cc);
            }
        }
    }

    /**
     * 获取资讯频道列表
     *
     * @param unitId
     * @return
     */
    public List<Channel> getNewsChannelList(int unitId) {
        return channelDao.getNewsChannelList(unitId);
    }

    /**
     * 根据类型获取频道列表
     *
     * @param unitId
     * @param channelType
     * @return
     */
    public List<Channel> getChannelList(int unitId, int channelType) {
        return channelDao.getChannelList(unitId, channelType);
    }

    /**
     * 获取所有频道列表
     *
     * @param unitId
     * @return
     */
    public List<Channel> getAllChannelList(int unitId) {
        return channelDao.getAllChannelList(unitId);
    }

    /**
     * 默认创建频道
     *
     * @param unit_id
     */
    @Transactional(readOnly = false)
    public void createDefault(int unit_id) {
        for (int i = 1; i < 5; i++) {
            Channel channel = new Channel();
            channel.setCreator(GlobalValue.SYS_USER_ID);
            channel.setUnit_id(unit_id);
            channel.setChannel_type(Channel.CHANNEL_TYPE_0);
            switch (i){
                case 1:
                    channel.setChannel_short(Channel.CHANNEL_SHORT_1);
                case 2:
                    channel.setChannel_short(Channel.CHANNEL_SHORT_2);
                case 3:
                    channel.setChannel_short(Channel.CHANNEL_SHORT_3);
                case 4:
                    channel.setChannel_short(Channel.CHANNEL_SHORT_4);
            }
            channel.setCreate_time(new Date());
            channel.setShow_channel(GlobalValue.NO);
            channelDao.save(channel);
        }
    }

    /**
     * 预览
     *
     * @param info
     * @param columns 栏目id
     * @return
     */
    public Object previewH5(Channel info, String columns) {
        List bannerList = new ArrayList();
        List infoList = new ArrayList();
        List columnList = new ArrayList();
        Map map = new HashMap();
        if (info.getIs_banner().intValue() == 1) {
            bannerList = informationService.listByChannelId(info.getId(), 0, 1);
        }
        // 热点新闻
        if (info.getChannel_type().intValue() == 1) {
            // 无栏目
            if (columns == null || columns.equals("")) {
                infoList = informationService.listByChannelId(info.getId(), 0, 0);

            } else {
                columns += ",";
                columnList = columnService.listByChannelId(columns);
                int columnsId = Integer.parseInt(columns.split(",")[0]);
                infoList = informationService.listByChannelId(info.getId(), columnsId, 0);
            }
        }

        map.put("bannerList", bannerList);
        map.put("infoList", infoList);
        map.put("columnList", columnList);

        return map;
    }

}
